package gui;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JApplet;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import network.GetOrderListWorker;
import network.NetworkManager;
import network.SaveOrderWorker;


import com.michaelbaranov.microba.Microba;

import data.DepartmentData;
import data.Order;
import data.ProductInfo;
import data.Order.OrderState;

public class AppletMenuBarDemo extends JApplet {
	private static final long serialVersionUID = -4767108041480605020L;

	public static final String ADMEL_DATA = "<html><br>ADMEL Adam Melnarowicz<br>ul. Gen. Boles\u0142awa Roi 28/11<br>05 - 119 Legionowo</html>";

	private LoginPanel loginPanel = null;
	private MainPanel mainPanel = null;
	private SuperUserPanel superUserPanel = null;
	private JMenuBar myMenu = null;
	private JMenu orderMenu = null;
	private JMenu modeMenu = null;

	private JMenuItem normalUserModeItem = null;
	private JMenuItem superUserModeItem = null;
	
	public void init() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		Microba.init();
		setSize(800, 600);

		loginPanel = new LoginPanel(this);
		getContentPane().add(loginPanel);
		getContentPane().revalidate();
		getContentPane().repaint();

		myMenu = new JMenuBar();
		JMenu mainMenu = new JMenu("Plik");
		orderMenu = new JMenu("Zam\u00F3wienie");
		modeMenu = new JMenu("Tryb pracy");
		
		JMenuItem newOrderItem = new JMenuItem("Nowe zam\u00F3wienie");
		JMenuItem openOrderItem = new JMenuItem("Otw\u00F3rz poprzednie zam\u00F3wienia");
		JMenuItem saveOrderItem = new JMenuItem("Zapisz zam\u00F3wienie");
		JMenuItem sendOrderItem = new JMenuItem("Wy\u015blij do akceptacji");
		
		JMenuItem logoutItem = new JMenuItem("Wyloguj");

		normalUserModeItem = new JMenuItem("Normalny u\u017cytkownik");
		superUserModeItem = new JMenuItem("Administrator");

		newOrderItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				mainPanel.resetOrder();
			}
		});
		openOrderItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				new GetOrderListWorker(mainPanel).execute();
			}
		});
		saveOrderItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				Order order = mainPanel.createOrder();
				if (order.getState() == OrderState.Submitted || order.getState() == OrderState.Accepted) {
					setCursor(Cursor.getDefaultCursor());
					JOptionPane.showMessageDialog(mainPanel, "Zam\u00f3wienie jest ju\u017c zapisane.", "B\u0142\u0105d", JOptionPane.ERROR_MESSAGE);
				} else if (order.validate())
					new SaveOrderWorker(mainPanel, order).execute();
				else {
					setCursor(Cursor.getDefaultCursor());
					JOptionPane.showMessageDialog(mainPanel, "Musisz uzupe\u0142ni\u0107 swoje zam\u00f3wienie przed zapisaniem.", "B\u0142\u0105d", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		sendOrderItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				Order order = mainPanel.createOrder();
				if (order.validate()) {
					order.setState(OrderState.Submitted);
					new SaveOrderWorker(mainPanel, order).execute();
				} else {
					setCursor(Cursor.getDefaultCursor());
					JOptionPane.showMessageDialog(mainPanel, "Musisz uzupe\u0142ni\u0107 swoje zam\u00f3wienie przed wys\u0142aniem.", "B\u0142\u0105d", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		logoutItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				myMenu.setVisible(false);
				getContentPane().removeAll();
				mainPanel = null;
				superUserPanel = null;
				getContentPane().add(loginPanel);
				getContentPane().revalidate();
				getContentPane().repaint();
			}
		});
		normalUserModeItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				switchMenuUserModes();
				getContentPane().remove(superUserPanel);
				getContentPane().add(mainPanel);
				getContentPane().revalidate();
				getContentPane().repaint();
			}
		});
		superUserModeItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				switchMenuUserModes();
				if (superUserPanel == null)
					superUserPanel = new SuperUserPanel();
				getContentPane().remove(mainPanel);
				getContentPane().add(superUserPanel);
				getContentPane().revalidate();
				getContentPane().repaint();
				superUserPanel.init();
			}
		});

		mainMenu.add(logoutItem);

		orderMenu.add(newOrderItem);
		orderMenu.add(openOrderItem);
		orderMenu.add(saveOrderItem);
		orderMenu.add(sendOrderItem);
		
		modeMenu.add(normalUserModeItem);
		modeMenu.add(superUserModeItem);

		myMenu.add(mainMenu);
		myMenu.add(orderMenu);
		myMenu.add(modeMenu);
		myMenu.setVisible(false);
		this.setJMenuBar(myMenu);
		NetworkManager.getInstance();
	}

	public void loggedIn(boolean isSuperUser, String user, String description, int image) {

		loginPanel.clearPassword();

		mainPanel = new MainPanel(this);
		enableOrderMenu(true);
		mainPanel.setImages(NetworkManager.BASE_URI + "img/" + image + ".png", "<html><br>" + description + "</html>",
				NetworkManager.BASE_URI + "img/admel_logo.png", ADMEL_DATA);
		getContentPane().add(mainPanel);

		List<DepartmentData> arr = NetworkManager.getInstance().getDepartments().getList();
		mainPanel.setObjects(arr);

		List<ProductInfo> products = NetworkManager.getInstance().getProductsData().getList();
		mainPanel.setProducts(products);
		
		myMenu.setVisible(true);
		setupSuperUserMenu(isSuperUser);
		setCursor(Cursor.getDefaultCursor());
		getContentPane().remove(loginPanel);
		getContentPane().revalidate();
		getContentPane().repaint();
	}
	
	private void setupSuperUserMenu(boolean isSuperUser) {
		modeMenu.setVisible(isSuperUser);
		normalUserModeItem.setEnabled(false);
	}
	
	private void switchMenuUserModes() {
		normalUserModeItem.setEnabled(!normalUserModeItem.isEnabled());
		superUserModeItem.setEnabled(!superUserModeItem.isEnabled());
	}
	
	public void enableOrderMenu(boolean enable) {
		orderMenu.setEnabled(enable);
	}
}